//index.js
//获取应用实例
const app = getApp()
const api = require('../../config/api.js')

Page({
  data: {
    pageSel: 1,
    test: false,
    curCpInfo: {
      "nickName":"小王",
      "age":"22",
      "gender":1,
      "location":"北京",
      "constellation":"水瓶",
      "height":"182",
      "avatarUrl":"/pic/avatar.jpg",
      "words":"当我慢慢变成回忆，你会在哪里",
      "labels":["第一","第二"],
      "aboutMe":"96年，湖南人，毕业于中传，现在北京就职于高校辅导员",
      "hobby":"闲暇之余喜欢到其他城市看一看不同的风光和生活状态，拍照看书画画 手工等 喜欢静态的活动，不喜欢运动出汗",
      "sentimentView":"",
      "loveTa":"向往简单的生活，喜欢美景美食，热爱美好的人与事，相信光哈哈哈,向往简单的生活，喜欢美景美食，热爱美好的人与事，相信光哈哈哈",
      "openid":"56156915615"
    },
    hasUserInfo: false,
    canIUse: qq.canIUse('button.open-type.getUserInfo'),
    opacity:1
  },
  //事件处理函数
  onLoad: function () {
    this.setData({

    })
  },
  onShow: function () {
    this.setData({

    })
  },
 
  getAuthorization: function() {
    qq.authorize({
      scope: 'setting.addFriend',
      success() {
        qq.showToast({
          title: '授权成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail(e) {
        console.error(e)
      }
    })
  },
  addFriendHandler: function(e) {
    qq.getSetting({
      success(res) {
        console.log("res getSetting", res)
      }
    })
    console.log(e)
  },

  profile: function(e) {
    this.setData({
      pageSel: 2
    })
    console.log(this.data)
  },


  getNewCP:function(){
    //把otherInfo里的第一个设置成curInfo，再从otherInfo里删去
    this.setData({
      curCpInfo:app.globalData.otherCpInfo[0]
    })
    app.globalData.otherCpInfo = app.globalData.otherCpInfo.slice(1)
    //请求匹配数
    let num = 20-app.globalData.otherCpInfo.length 
    qq.request({
      url: api.MatchCP,
      data: {"num":num},
      header: {
        'Content-Type': 'application/json',
        'Xworld-Token': app.globalData.token
      },
      method: 'POST',
      success: res => {
        console.log('res success getNewCP', res)
        //把收到的Info循环合并添加到otherInfo
        for (i=0;i<res.data.cpInfo.length;i++){
          //如果有xuserInfo就合并后push，没有就直接push
          if (res.data.cpInfo[i].xuserInfo) {
            app.globalData.otherCpInfo.push(Object.assign(res.data.cpInfo[i].userInfo, res.data.cpInfo[i].xuserInfo))
          } else {
            app.globalData.otherCpInfo.push(res.data.cpInfo.userInfo[i])
          }
        }
      },
      fail: res => {
        console.log('res fail', res)
      }
    })
  },

  setOpacity:function(){
    let that=this
    let opacity = 1
    for (i=0;i<100;i++){
      setTimeout(function(){
        opacity-=0.01
        that.setData({
          opacity:opacity
        })
      },20)
    }
    setTimeout(function(){
      let randomNum = Math.ceil(Math.random()*2+2) //2到4
      if (app.globalData.otherCpInfo.length < randomNum){
        that.getNewCP()
      } else {
        that.setData({
          curCpInfo:app.globalData.otherCpInfo[0]
        })
      }   
      setTimeout(function(){
        for (i=0;i<100;i++){
          setTimeout(function(){
            opacity+=0.01
            that.setData({
              opacity:opacity
            })
          },500)
        }
      })
    },500) 
  },

  unLikeCp:function(){
    this.setOpacity()
  },


  likeCP:function(){
    let curCpInfo = this.data.curCpInfo
    app.globalData.iLikesNum += 1
    if (app.globalData.iLikes) {
      app.globalData.iLikes.unshift(curCpInfo)
    } else {
      app.globalData.iLikes = [curCpInfo]
    }
    
    //TODO api/syncIlikes

    //下一个cp
    this.setOpacity()
    
  }

})